<?php
namespace plugins\goods\controller;

use cmf\controller\ApiPluginBaseController; //引用插件基类
use think\Db;

/**
 * api控制器
 */
class ApiGoodsFootprintController extends ApiPluginBaseController
{
    /**
     * 我的足迹
     * @param  uid  用户id
     * @param  time   时间
     * @return \返回json字符串
     */
    public function myTracks()
    {
        $data  = $this->request->param();
        $uid   = $this->getIdentity();
        $uid   = json_decode(json_encode($uid), true);
        $uid   = $uid['uid']; //用户id
        $page = !empty($data['page']) ? $data['page'] : 1;
        $pageNum = !empty($data['pageNum']) ? $data['pageNum'] : 5;

        $date  = strtotime("-1 months", time()); //前个月的时间戳
        $where = 'uid="' . $uid . '" AND create_time>=' . $date;
        $field = 'id,footprint_time,create_time';

        //获取分页
        $count = db('goods_footprint')->where($where)->field($field)->group('footprint_time')->count();
        $total = $count;
        $per_page = $pageNum;
        $current_page = $data['page'];
        $last_page = ceil($count / $per_page);

        if ($page > $last_page) {
            return api_json_echo(false, '没有该分页商品', null, 100);
        }

        //天数
        $footprint_time = db('goods_footprint')->where($where)->order('footprint_time', 'desc')->group('footprint_time')->column('footprint_time');
        if (!empty($footprint_time)) {
            //获取选择页的时间
            $k = (($page * 5) -1 - 5) > 0 ?: 0;
            $start = !empty($footprint_time[$k]) ? $footprint_time[$k] : '';
            $end = !empty($footprint_time[$k + 5]) ? $footprint_time[$k + 5] : '';


            $where .= " and footprint_time <= ".$start;
            if (!empty($end)) {
                $where .= " and footprint_time >".$end;
            }
        }

        //获取查询页数的开始时间和结束时间
        $info  = db('goods_footprint')->where($where)->order('footprint_time','desc')->group('footprint_time')->field($field)->select();
        $info  = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        $info  = json_decode($info, true);
        if (empty($info)) {
            return api_json_echo(false, '用户没有数据', '', 101);
        }
        $item        = array();
        $info_unique = $info;
        // $info_unique = $this->assoc_unique($info, 'footprint_time');
        foreach ($info_unique as $key => $value) {
            $item[$key]['create_time'] = date('Y-m-d', $value['footprint_time']);
            $where1                    = 'uid="' . $uid . '" AND create_time>=' . $date . ' and footprint_time=' . $value['footprint_time'];
            $item[$key]['data']        = db('goods_footprint')->where($where1)->order('create_time','desc')->field('id,thumb,title,price,goodsid')->select();
        }


        $da = [
            'data' => $item,
            'total' => $total,
            'per_page' => $per_page,
            'current_page' => $current_page,
            'last_page' => $last_page,
        ];

        return api_json_echo(true, '查询成功', $da, 200);
    }

    /**
     * 足迹前十
     */
    public function tracksTen()
    {
        $data  = $this->request->post();
        $uid = $this->getIdentity()->uid;
        $date  = strtotime("-1 months", time()); //前个月的时间戳
        $where = 'uid="' . $uid . '" AND create_time>=' . $date;
        $field = 'id,thumb,title,price,goodsid';
        $info  = db('goods_footprint')->where($where)->field($field)->order('create_time','desc')->limit(10)->select()->toArray();

        return api_json_echo(true, '查询成功', $info, 200);
    }



    private function assoc_unique($arr, $key)
    {
        $tmp_arr = array();
        foreach ($arr as $k => $v) {
            if (in_array($v[$key], $tmp_arr)) {
//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                unset($arr[$k]);
            } else {
                $tmp_arr[] = $v[$key];
            }
        }
        sort($arr); //sort函数对数组进行排序
        return $arr;
    }

    /**
     * 删除足迹
     */
    public function deleteFootprint()
    {
        $param = $this->request->param();
        if (empty($param['id'])) {
            return api_json_echo(false, '缺少参数', '', 100);
        }
        $res = db('goods_footprint')->where('id', $param['id'])->delete();
        if ($res) {
            return api_json_echo(true, '删除', '', 200);
        }

        return api_json_echo(false, '失败', '', 101);
    }
}
